package com.cms.recursoshumanos.facade;

import net.rim.device.api.system.Bitmap;

import com.cms.recursoshumanos.component.base.URLBitmapField;
import com.cms.recursoshumanos.constant.Constant;
import com.cms.recursoshumanos.engine.ScreenEngine;
import com.cms.recursoshumanos.handler.WServiceCall;
import com.cms.recursoshumanos.json.me.JSONObject;
import com.cms.recursoshumanos.object.Comida;

public class BaseFacade implements Runnable 
{

	//public static ResourceBundle rscBundle = ResourceBundle.getBundle(BUNDLE_ID, BUNDLE_NAME);
	public final static String NUM_ITEMS = "50";
	boolean forSearch = false;
	
	String page="1";	// Only for lists!
	
	private final String RESPONSE_CODE = "responseCode";
	
	public String whatIsTheError(String jsonString) {
		if(jsonString.compareTo(WServiceCall.ERROR_CONNECTION) == 0)
			return "Error de Coneccion";
		int error = -1;
		
		try {
			JSONObject jsonObject = new JSONObject(jsonString);
			
			if (jsonObject != null)
				error = jsonObject.getInt(RESPONSE_CODE);
		
			if(error == WServiceCall.EXITO)
				return WServiceCall.OK;
			if(error == WServiceCall.NO_RECORD)
				return "No se Encotro Resultados";
			if(error == WServiceCall.ERROR_BD || error == WServiceCall.ERROR_NO_PERMIT)
				return "Error del Provider";
			if(error == WServiceCall.ERROR_CREDENTIALS || error == WServiceCall.ERROR_CREDENTIALS2 || error == WServiceCall.ERROR_NO_PERMIT)
				return "Error de Credenciales";
		} catch (Exception e) {
			System.out.println(e.getMessage());
			
		}; // Return error Weirdo
		
		return "Error inexplicable";
	}

	public void setNextPage() {
		this.page = ""+(Integer.parseInt(page)+1);
		System.out.println(page);
	}
	
	public void run() {}; // Implement in his children!
	
	public void fromStart() {
		page = "1";		
	}
	
	public void setForSearch(boolean forSearch) {
		this.forSearch = forSearch;
	}
	
	public boolean isForSearch() {
		return forSearch;
	}
	public void run_facade(final BaseFacade runThis) 
	{
		if (!(runThis instanceof ImagenesFacade))
		{
			ScreenEngine.getInstance().showLoadingPopup("Cargando");
		}
		Thread threadToRun = new Thread()
		{
	        public void run() 
	        {	
	        	
	        	runThis.run();	// Corre el Facade
    		  
	        }
	        
	        
		};
		threadToRun.start();
    }
	
	public Bitmap cargar_imagen(int tipo, Object elObjeto)
    {
    	URLBitmapField post_img = new URLBitmapField(Constant.DIR_FOTO_BLANK);
    	switch (tipo) 
    	{
			case 1: // comida
				
				Comida laComida = (Comida) elObjeto;
				synchronized (post_img) 
				{
					post_img= new URLBitmapField(Constant.URL_COMIDA+laComida.getIdComida()+
							"/"+laComida.getNombreComida());
					try
					{
						post_img.getBitmap();
					}
					catch (NullPointerException ex)
					{
						post_img = new URLBitmapField(Constant.DIR_FOTO_BLANK);
					}
				}
				
				break;
				
			/*case 2: // Catalogos_Articulo
				
				Articulo elArticulo = (Articulo) elObjeto;
				synchronized (post_img) 
				{
					post_img= new URLBitmapField(elArticulo.getUrlFoto());
					try
					{
						post_img.getBitmap();
					}
					catch (NullPointerException ex)
					{
						post_img = new URLBitmapField(Constant.URL_FOTO_BLANK);
					}
				}
				
				break;*/
	
			default:
				break;
		}
    	return post_img.getBitmap();
    }
}

